package mobile.pubcrawler.ui;

import mobile.pubcrawler.PubCrawlerUI;
import mobile.pubcrawler.R;
import mobile.pubcrawler.RatingsListAdapter;
import mobile.pubcrawler.model.DataModel;
import mobile.pubcrawler.model.DataModelLocation;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

public class PubDetails extends Activity {
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        int position = getIntent().getIntExtra("position", 0);
        DataModelLocation location = DataModel.getInstance().getLocationList().get(position);
        
        setContentView(R.layout.pubdetails);
        this.setTitle(R.string.pubDetailsActivityTitle);
    	
        ImageView logo = (ImageView)findViewById(R.id.details_logo); 
        logo.setImageResource(PubCrawlerUI.getInstance().getImageResource(location.getCategory()));
        
        TextView description = (TextView)findViewById(R.id.details_description); 
        description.setText(Html.fromHtml(location.getName()));
        description.setTextSize(16);
        
        TextView address = (TextView)findViewById(R.id.details_address); 
        address.setText(Html.fromHtml(location.getAddress()+"<br>"+location.getPostalCode()+" "+location.getCity()));
        
        TextView distance = (TextView)findViewById(R.id.details_distance); 
        distance.setText(String.valueOf(location.getDistance())+"m");
        
        TextView phone = (TextView)findViewById(R.id.details_phone); 
        String info="";
        String phone1 = location.getPhone1();
        String phone2 = location.getPhone2();
        if (phone1 != null) info+=phone1;
        if (phone2 != null) info+=", "+phone2;
        phone.setText(Html.fromHtml(info));
        
        TextView category = (TextView)findViewById(R.id.details_category); 
        category.setText(location.getCategory());
        
        RatingBar rating = (RatingBar)findViewById(R.id.details_small_ratingbar);
        rating.setRating(location.getOverallRating());
        
        TextView reviews = (TextView)findViewById(R.id.details_PubReviews);
        reviews.setText(location.getRating().size()+" "+getString(R.string.detailsNumReviews));
                
    	ListView ratingsList = (ListView)findViewById(R.id.details_ratingsListView);
    	RatingsListAdapter ratingsAdapter = new RatingsListAdapter(this, location.getRating());
    	ratingsList.setAdapter(ratingsAdapter);
        
	 }
	 
	    //----------------- Menu inflaters and listeners -------------------
	    public boolean onCreateOptionsMenu(Menu menu) {
	        MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.menu.mainmenu, menu);
	        return true;
	    }
	    
	    /* Handles item selections */
	    public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) {
	        case R.id.menuMainMenu:
	        	PubCrawlerUI.getInstance().startMainUI();
	            return true;
	        case R.id.menuSettings:
	        	PubCrawlerUI.getInstance().startSettingsUI();
	            return true;
	        case R.id.menuQuit:
	            System.exit(0);
	            return true;
	        }
	        return false;
	    }
	 
}
